$(function () {
    //用的比较多的一些标签
    let $first = $("#comment .pagination").find("select:first");//每页几条的标签
    let $last = $("#comment .pagination").find("select:last");//跳转到指定页
    //改变星级
    $(".res-com span").on("click", function () {
        $(this).toggleClass("act");
        if ($(this).attr("class") == "act") {
            $(this).prevAll().addClass("act");
        } else {
            $(this).nextAll().removeClass("act");
        }
    });
    //封装的展示的评论方法
    let Review = function (data) {
        $("#comment b").text(data.obj.total);//展示该文件下评论总数
        //移除之前展示的评论
        $.each($("#comment>div"), function (i, n) {
            let $n = $(n);
            if ($n.attr("class") == "cons") {
                $n.remove();
            }
        });
        //展示评论
        $("#comment .pagination").before(data.obj.comments.map(function (obj) {
            let starLevel = +obj.starLevel;//星级数
            let str = "";//拼上星级
            for (let i = 0; i < starLevel; i++) {
                str += `<span class="act">★</span>`;
            }
            for (let i = 0; i < 5 - starLevel; i++) {
                str += `<span>★</span>`;
            }
            let headerimg = "http://192.168.17.12:8060/"+obj.headerimg;//发送评论用户的头像
            let name = obj.nickname;//发送评论用户的名字
            if (name == null) {
                name = obj.tel;//发送评论用户的电话
            }
            return `<div class="cons">
                        <div class="img">
                            <img src="${headerimg}"/>
                            <span class="star">${str}</span>
                        </div>
                        <div class="cmt">
                            <span>${name}</span>|<span>${obj.time}</span>
                            <p class="msg">
						        <span>${obj.content}</span>
                            </p>
                        </div>
                    </div>`;
        }).join(""));
        //跳转至页面顶部
        $('html, body').stop().animate({
            scrollTop: 0
        });
    }
    //封装的展示评论底部按钮方法
    let ReviewPaging = function (data) {
        $("#comment .pagination").find("a").remove();//移除之前展示的按钮
        $last.empty();//移除跳转到指定页里面的选项
        let page = data.obj.page;//一共多少页
        if (page == 0) {
            $("#comment .pagination").find("select").hide();
        } else {
            $("#comment .pagination").find("select").show();
        }
        if (page != 0) {
            $last.before(
                `<a class="left"></a>`
            );
        }
        if (page <= 3) {
            //小于等于3页展示全部页
            for (let i = 1; i <= page; i++) {
                $last.before(
                    `<a>${i}</a>`
                );
            }
        } else {
            //大于3页展示前3页
            for (let i = 1; i <= 3; i++) {
                $last.before(
                    `<a>${i}</a>`
                );
            }
        }
        if (page != 0) {
            $first.next().next().addClass("act");//默认展示第一页
            $last.before(
                `<a class="right"></a>`
            );
        }
        for (let i = 1; i <= page; i++) {
            $last.append(
                `<option>${i}/${page}</option>`
            );
        }
    }
    //封装的切换评论底部按钮数字的方法
    let ReviewPagingToggle = function (flag, which) {
        if (flag == 1) {//按钮要前移
            let text = +$first.next().next().text();
            if (which != null) {
                text = which + 1;
            }
            $("#comment .pagination").find("a").remove();//移除之前展示的按钮
            $last.before(
                `<a class="left"></a>`
            );
            for (let i = text - 1; i <= text + 1; i++) {
                $last.before(
                    `<a>${i}</a>`
                );
            }
            $last.before(
                `<a class="right"></a>`
            );
            $first.next().next().addClass("act");
        } else if (flag == 2) {//按钮要后移
            let text = +$last.prev().prev().text();
            if (which != null) {
                text = which - 1;
            }
            $("#comment .pagination").find("a").remove();//移除之前展示的按钮
            $last.before(
                `<a class="left"></a>`
            );
            for (let i = text - 1; i <= text + 1; i++) {
                $last.before(
                    `<a>${i}</a>`
                );
            }
            $last.before(
                `<a class="right"></a>`
            );
            $last.prev().prev().addClass("act");
        }
    }
    //提交评论
    $(".res-com button").on("click", function () {
        $(".promptMessage").css("backgroundColor", "#FEF0F0")
        $(".promptMessage").css("color", "#F56C6C");
        $(".promptMessage img").prop("src", "images/error.png");
        let number = 0;//评论的星级数
        $.each($(".res-com span"), function (i, n) {
            if ($(n).attr("class") == "act") {
                number++;
            }
        });
        if ($(".res-com .area").val().trim().length >= 5) {
            $.get(
                "review/uploadReview",
                {
                    href: location.href,
                    review: $(".res-com .area").val().trim(),//评论内容
                    count: number//评论星级数
                },
                function (data) {
                    $(".promptMessage span").text(data.msg);
                    if (data.code == "631") {
                        $(".promptMessage").css("backgroundColor", "#F0F9EB")
                        $(".promptMessage").css("color", "#67C23A");
                        $(".promptMessage img").prop("src", "images/success.png");
                        //刷新页面
                        $.get(
                            "review/dataReview",
                            {
                                href: location.href,
                                rows: $first.val()//每页条数
                            },
                            function (data) {
                                Review(data);
                                ReviewPaging(data);
                            }
                        );
                    }
                }
            );
        } else if ($(".res-com .area").val().trim() == 0) {
            $(".promptMessage span").text("评论的内容不能为空");
        } else {
            $(".promptMessage span").text("评论的内容少于5个字符");
        }
        //3秒后提示信息清除
        $(".promptMessage").slideDown();
        setTimeout(function () {
            $(".promptMessage").slideUp();
        }, 3000);
    });
    //页面加载评论
    $.get(
        "review/dataReview",
        {
            href: location.href,
            rows: $first.val()//每页条数
        },
        function (data) {
            Review(data);
            ReviewPaging(data);
        }
    );
    //评论每页条数
    $first.on("change", function () {
        $.get(
            "review/dataReview",
            {
                href: location.href,
                rows: $(this).val()//每页条数
            },
            function (data) {
                Review(data);
                ReviewPaging(data);
            }
        );
    });
    //评论换页
    $("#comment .pagination").on("click", "a", function () {
        let flag = 0;//用来定义一下规则(1:页面按钮需要前移,2:页面按钮需要后移)
        let text = $(this).text();//第几页
        if ($(this).attr("class") == null || $(this).attr("class") == "") {//点击的数字按钮
            $(this).addClass("act").siblings("a").removeClass("act");
        } else if ($(this).attr("class").indexOf("left") >= 0) {//点击的上一页
            $.each($(this).siblings("a"), function (i, n) {
                let obj = $(n);
                if (obj.attr("class") == "act") {//获取到当前页对应按钮
                    let t = +obj.text();//当前是第几页
                    if (obj.prev().attr("class") == null || obj.prev().attr("class") == "") {//当前页面按钮不是第一个按钮
                        text = t - 1;
                        obj.prev().addClass("act").siblings("a").removeClass("act");
                        return false;
                    } else if (t > 1) {//当前页面按钮是第一个按钮,但不是第一页
                        text = t - 1;
                        flag = 1;
                        return false;
                    }
                }
            });
        } else if ($(this).attr("class").indexOf("right") >= 0) {//点击的下一页
            $.each($(this).siblings("a"), function (i, n) {
                let obj = $(n);
                if (obj.attr("class") == "act") {//获取到当前页对应按钮
                    let t = +obj.text();//当前是第几页
                    if (obj.next().attr("class") == null || obj.next().attr("class") == "") {//当前页面按钮不是最后一个按钮
                        text = t + 1;//当前是第几页
                        obj.next().addClass("act").siblings("a").removeClass("act");
                        return false;
                    } else if (t < $last.val().split("/")[1]) {//当前页面按钮是第一个按钮,但不是第一页最后一页
                        text = t + 1;
                        flag = 2;
                        return false;
                    }
                }
            });
        }
        if (text == "") {
            return;
        } else {
            $.each($last.children(), function (i, n) {
                let obj = $(n);
                if (text == obj.text().split("/")[0]) {
                    obj.prop("selected", true);
                }
            });
        }
        $.get(
            "review/dataReview",
            {
                href: location.href,
                rows: $first.val(),
                whichPage: text
            },
            function (data) {
                Review(data);
                ReviewPagingToggle(flag, null);
            }
        );
    });
    //评论跳转至指定页
    $last.on("change", function () {
        let flag = 0;//用来定义一下规则(1:页面按钮需要前移,2:页面按钮需要后移)
        let which = +$(this).val().split("/")[0];//跳转到第几页
        $.each($first.siblings("a"), function (i, n) {
            if ($(n).text() == which) {
                $(n).addClass("act").siblings("a").removeClass("act");
            }
        });
        if (which > $last.prev().prev().text()) {
            flag = 2;
        }
        if (which < $first.next().next().text()) {
            flag = 1;
        }
        $.get(
            "review/dataReview",
            {
                href: location.href,
                rows: $first.val(),
                whichPage: $(this).val()
            },
            function (data) {
                Review(data);
                ReviewPagingToggle(flag, which);
            }
        );
    });
    //页面加载文件内容
    $.get(
        "review/dataFileContent",
        {
            href: location.href
        },
        function (data) {
            let o = data.obj;
            let str = "";
            if (o.f_lable != null) {
                let arr = o.f_lable.split("-");
                for (let i = 0; i < arr.length; i++) {
                    str += `<em>${arr[i]}</em>`;
                }
            }
            $("#detail .msg").before(
                `<div class="title">
                    <h1>${o.f_title}<a href="javascript:void(0)"></a></h1>
                    <b class="time">${o.f_uploadTime}</b>
                    <b class="filesize">${o.f_size}MB</b>
                    <span class="kws">${str}</span>
                </div>
                <div class="desc">${o.f_describe}</div>`
            );
            $("#detail .oper").find("b:first").text(o.f_integral);
            $("#detail .oper").find("b:last").text(o.f_total);
        }
    );
    //页面加载用户信息
    $.get(
        "review/dataUserMsg",
        function (data) {
            let tel = data.tel;
            tel=tel.substring(0,3)+"****"+tel.substring(7);
            $(".users>p").before(
                `<img src="http://192.168.17.12:8060/${data.headerimg}"/>
                <div>
                    <p>
                        <a href="javascript:void(0);">${data.nickname}</a>
                        <a class="modify" href="./modifypass.html">修改密码</a>
                    </p>
                    <p className="u">
                        <a href="javascript:void(0);">${data.realname} |</a>
                        <a href="javascript:void(0);">${tel} |</a>
                        <a href="javascript:void(0);">${data.sex} |</a>
                        <a href="javascript:void(0);">${data.birthday}</a>
                    </p>
                </div>`
            );
        }
    );
});